package cn.itsource.rpms.service.impl;

import cn.itsource.rpms.mapper.BaseMapper;
import cn.itsource.rpms.query.BaseQuery;
import cn.itsource.rpms.service.IBaseService;
import cn.itsource.rpms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class BaseServiceImpl<T> implements IBaseService<T>{

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public List<T> queryAll() {
        return mapper.queryAll();
    }

    @Override
    public T queryOne(Serializable id) {
        return mapper.queryOne(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);

    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Serializable id) {
        mapper.delete(id);
    }

    @Override
    @Transactional
    public void batchDelete(List ids) {
        mapper.batchDelete(ids);
    }

    @Override
    public PageResult<T> queryPage(BaseQuery baseQuery) {
        PageResult<T> pageList = new PageResult<T>();
        //总数 select count(*) from xxx where ?
        Long total =  mapper.queryTotal(baseQuery);
        //select * from xxx where xxx limit
        List rows =  mapper.queryData(baseQuery);
        pageList.setTotal(total);
        pageList.setRows(rows);
        return pageList;
    }
}
